#!/bin/ash
#perform background housekeeping.

#exec &>/tmp/${0##*/}.log ; set -x #debug

if ! /usr/local/pup_event/frontend_startup $@ ; then
	exit 1
fi

rm -f /tmp/services/pup_event_timeout

. /etc/eventmanager #has RAMSAVEINTERVAL, HOTPLUGNOISY, BACKENDON, POWERTIMEOUT
. /etc/rc.d/PUPSTATE

#===============================================

MINUTE=0;
SAVECNT=0
MOUSECNT=0
CURPOS1=""
CURPOS2=""

while [ 1 ]
do

	sleep 60
	MINUTE=$((MINUTE+1))

	. /etc/eventmanager
	[ "$RAMSAVEINTERVAL" = "" ] && RAMSAVEINTERVAL=60
	[ "$POWERTIMEOUT" = "" ] && POWERTIMEOUT=0

	if [ "$PUPMODE" = "13" ] ; then
		SAVECNT=$(( $SAVECNT + 1 ))
		if [ $RAMSAVEINTERVAL -ne 0 -a $SAVECNT -ge $RAMSAVEINTERVAL ];then
			if save2flash pup_event ; then #periodic save of tmpfs top layer...
				SAVECNT=0
			fi
		fi
	fi

	if [ $POWERTIMEOUT -ne 0 ];then #power-off computer after inactivity.
		MOUSECNT=$(( $MOUSECNT + 1 ))
		CURPOS2="`getcurpos`"
		if [ -z "$CURPOS1" ] ; then
			CURPOS1="$CURPOS2"
		fi
		if [ "$CURPOS1" != "$CURPOS2" ];then
			MOUSECNT=0
		fi
		CURPOS1="$CURPOS2"
		[ $MOUSECNT -ge $POWERTIMEOUT ] && wmpoweroff
	fi

done

### END ###
